package com.ccxe.manage.dao.impl;

import java.sql.ResultSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ccxe.manage.db.CcxDB;
import com.ccxe.manage.db.CcxDBOracle;
import com.ccxe.manage.db.DBFactory;
import com.ccxe.manage.service.CurrUserInfo;

public class UserLoginDao extends CcxDBOracle {

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(UserLoginDao.class);

	public UserLoginDao() throws Exception {
		super();
	}

	public CurrUserInfo getCurrUserInfo(String userId) throws Exception {
		String sql = " select u.user_id,u.user_name,u.customer_id,u.user_type,u.user_disabled,r.role_id,p.password from C_USER  u "
				+ " left join C_USER_ROLE r on u.user_id = r.user_id "
				+ " left join C_USER_PASSWORD p on u.user_id = p.user_id "
				+ " where u.user_id = ?";

		CcxDB db = null;
		CurrUserInfo userInfo = new CurrUserInfo();
		ResultSet rst = null;
		db = DBFactory.getInstance();
		Object[] obj = new Object[] { userId };
		try {
			rst = executeQuery(sql, obj);
			if (rst.next()) {
				userInfo.setUserId(rst.getString("USER_ID"));
				userInfo.setUserName(rst.getString("USER_NAME"));
				userInfo.setCusId(rst.getString("CUSTOMER_ID"));
				userInfo.setUserType(rst.getString("USER_TYPE"));
				userInfo.setRoleID(rst.getString("ROLE_ID"));
				userInfo.setPassword(rst.getString("PASSWORD"));
				userInfo.setUserDisable(rst.getString("USER_DISABLED"));
			}
			rst.close();
			db.close();
			if (userInfo != null) {
				return userInfo;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rst != null)
				rst.close();
			if (db != null)
				db.close();
		}
		return null;
	}
}
